home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / dev / misc / LEDA_gene.lha / LEDA-3.1c-generic / incl / LEDA / impl / dp_hash.h < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-05  |  4.6 KB  |  225 lines

  1. /*******************************************************************************
  2. +
  3. +  LEDA  3.1c
  4. +
  5. +
  6. +  dp_hash.h
  7. +
  8. +
  9. +  Copyright (c) 1994  by  Max-Planck-Institut fuer Informatik
  10. +  Im Stadtwald, 6600 Saarbruecken, FRG     
  11. +  All rights reserved.
  12. *******************************************************************************/
  13.  
  14.  
  15. #ifndef LEDA_DP_HASH_H
  16. #define LEDA_DP_HASH_H
  17.  
  18. //------------------------------------------------------------------------------
  19. // Dynamic Perfect Hashing
  20. //
  21. // Michael Wenzel          ( 1990/91 )
  22. //------------------------------------------------------------------------------
  23.  
  24.  
  25. #include <LEDA/basic.h>
  26.  
  27.  
  28. // ---------------------------------------------------------------
  29. // declarations
  30. // ---------------------------------------------------------------
  31.  
  32.  
  33. class headertable;
  34. class subtable;
  35. class dp_hash;
  36.  
  37. typedef headertable* htp;
  38. typedef subtable* stp;
  39.  
  40. #define EMPTY (GenPtr(2147483648))
  41. #define maxuni 2147483647
  42.                                 // #define maxprim 2147483659
  43.                 // aber kleiner wegen random-Funktion
  44. #define maxprim 2147483647
  45. #define wursechs 2.44948974278
  46.  
  47.  
  48.  
  49.  
  50. // --------------------------------------------------------------
  51. // class headertable
  52. //
  53. // Items werden auf Headertables gehasht,
  54. // die sie dann injektiv auf Subtables verteilen
  55.  
  56. class headertable {
  57.  
  58.   unsigned kj;                     // Multiplikator
  59.   int wj;                          // Anzahl Elemente in Tafel
  60.   int mj;                          // max. Anzahl Elemente
  61.   stp tj;                          // Startpunkt der Subtables
  62.  
  63.   friend class b_dict;
  64.   friend class dp_hash;
  65.   friend class subtable;
  66.  
  67.   stp lookup(GenPtr );
  68.  
  69.   bool insert(GenPtr ,GenPtr ,stp& ,int& ,bool& ,stp ,stp );
  70.   int dinsert(GenPtr ,GenPtr ,stp ,stp& ,stp& );
  71.  
  72.   bool del(GenPtr ,stp ,stp ); 
  73.   int give_elements(stp& ,stp ,stp );  
  74.  
  75.  
  76.   headertable()
  77.     { 
  78.       kj = wj = mj=0; 
  79.       tj=0;   
  80.      }
  81.  
  82. };
  83.  
  84. // --------------------------------------------------------------
  85. // class subtable
  86. //
  87. // Jedes Subtableelement ist leer,
  88. // oder von der Headertable wird genau ein Element    
  89. // auf eine Position gehasht
  90.  
  91. class subtable {
  92.  
  93.   GenPtr ke;
  94.   GenPtr inf;
  95.  
  96.   friend class b_dict;
  97.   friend class dp_hash;
  98.   friend class headertable;
  99.  
  100.   void set_s(GenPtr a,GenPtr b)
  101.     {
  102.        ke=a; inf=b; }
  103.  
  104.   void clear()
  105.     { 
  106.        ke=EMPTY; inf=0; }
  107.  
  108.   subtable()
  109.     {  
  110.        ke=EMPTY; inf=0; }
  111.  
  112.   subtable(GenPtr a ,GenPtr b )
  113.     {  
  114.        ke=a; inf=b; }
  115.  
  116.   subtable(subtable& s)
  117.     {  
  118.        ke=s.ke;
  119.        inf=s.inf; }
  120.  
  121.    subtable& operator=(subtable& s)
  122.     {
  123.        ke=s.ke;
  124.        inf=s.inf;
  125.        return *this; }
  126.  
  127.  
  128.   public:
  129.  
  130.   GenPtr key() { return ke; }
  131.  
  132.   GenPtr info() { return inf; }
  133.  
  134. };
  135.  
  136. // ------------------------------------------------------------------
  137. // class dp_hash
  138. //
  139. // alle Informationen fuer das Dictionary
  140. // Elemente werden auf Headertables gehasht, 
  141. // die dann die Elemente weitergeben
  142. // Der Platzverbrauch der Headertables wird kontrolliert
  143.  
  144.  
  145. class dp_hash {
  146.  
  147.   htp* htablep;      // Feld von Zeigern auf Headertables
  148.              // leere Headertables werden nicht angelegt
  149.  
  150.   unsigned k;        // Verteilungsfunktion (k*x)%p%sM
  151.  
  152.   int n;             // Anzahl Elemente im Dictionary
  153.  
  154.   int M;             // Anzahl Elemente, die momentan bzgl.
  155.              // Platzverbrauch mit Wahrscheinlichkeit >= 0.5
  156.              // abgespeichert werden koennen ( ohne Rehash )
  157.  
  158.   int sM;            // Anzahl Headertables
  159.  
  160.   int bed;           // Kontrolle des Platzverbrauchs
  161.  
  162.   stp anf,wo,ende;   // zur Speicherverwaltung beim Rehash
  163.  
  164.  
  165.  
  166.   stp  rehash_all(GenPtr ,GenPtr );
  167.  
  168.  
  169.   virtual void clear_key(GenPtr&)const {}
  170.   virtual void clear_inf(GenPtr&)const {}
  171.  
  172.   virtual void copy_key(GenPtr&) const {}
  173.   virtual void copy_inf(GenPtr&) const {}
  174.  
  175. protected:
  176.  
  177.   stp item(void* p) const { return stp(p); }
  178.  
  179. public:
  180.  
  181.   stp first_item() const
  182.   { error_handler(1,"sorry, dp_hash::first_item not implemented");
  183.     return nil;
  184.    }
  185.  
  186.   stp next_item(stp) const
  187.   { error_handler(1,"sorry, dp_hash::next_item not implemented");
  188.     return nil;
  189.    }
  190.  
  191.   dp_hash& operator=(const dp_hash&) 
  192.   { error_handler(1,"sorry, dp_hash::operator= not implemented");
  193.     return *this;
  194.    }
  195.  
  196.   GenPtr  key(stp it)  const { return it->ke; }
  197.   GenPtr  inf(stp it)  const { return it->inf; }
  198.   GenPtr& info(stp it) const { return it->inf; }
  199.  
  200.   stp  insert(GenPtr,GenPtr );
  201.   void del(GenPtr);
  202.  
  203.   void del_item(stp it) { del(it->ke); }
  204.  
  205.   stp  lookup(GenPtr ) const;
  206.  
  207.   stp  change_inf(GenPtr ,GenPtr );
  208.  
  209.   int  size() const { return n; }
  210.  
  211.   void clear();
  212.  
  213.  
  214.   dp_hash();
  215.   dp_hash(int ,GenPtr* ,GenPtr* );
  216.   virtual ~dp_hash();
  217.  
  218.  
  219.   friend class b_dict;
  220.  
  221. };
  222.  
  223. #endif
  224.